﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CSquareEmitter))]
    public class CSquareEmitter: AEmitter
    {

        #region properties

        public Vector2 Rect { get; set; }
        public bool EdgeSpawn { get; set; }

        #endregion

        #region ctor

        public CSquareEmitter(string name)
            :base(name)
        {

        }

        #endregion

        #region methods

        public override AEmitter Clone()
        {
            CSquareEmitter pe = new CSquareEmitter(Name);
            pe.EmissionInterval = EmissionInterval;
            pe.EmitPerEmission = EmitPerEmission;
            pe.MaxParticlesSimultaneous = MaxParticlesSimultaneous;
            pe.ReferenceParticle = ReferenceParticle;
            pe.Rect = Rect;
            pe.EdgeSpawn = EdgeSpawn;
            return pe;
        }

        public override void GetParametersFromEmitter(IParticle particle)
        {
            m_transformNode.Position = m_node.Position;
            m_transformNode.Orientation = m_node.Orientation;
            Vector2 rect = Rect;
            Vector3 posOff = m_node.Position;
            float x = CDare.Instance.Random.Next((int)(-rect.X * 1000), (int)(rect.X * 1000)) / 1000.0f;
            float y = 0;
            float z = CDare.Instance.Random.Next((int)(-rect.Y * 1000), (int)(rect.Y * 1000)) / 1000.0f;
            Vector3 vec = Vector3.One;//Vector3.Normalize(particle.Position - m_node.Position);

            if (!EdgeSpawn)
                particle.Position = new Vector3(posOff.X + x, posOff.Y, posOff.Z + z);
            else
            {
                if (CDare.Instance.Random.Next(2) == 0)
                    x = rect.X * (CDare.Instance.Random.Next(2) == 0 ? 1 : -1);
                else
                    z = rect.Y * (CDare.Instance.Random.Next(2) == 0 ? 1 : -1);
            }
            m_transformNode.Translate(new Vector3(x, 0, z), CNode.ESpace.LOCAL);

            particle.Position = m_transformNode.Position;
            Vector3 dir = vec * Velocities;

            particle.Velocities = dir;
            
        }
        #endregion
    }
}
